package com.gxa.eshop.admin.controller;

import com.gxa.eshop.admin.service.GoodsService;
import com.gxa.eshop.core.exception.BusinessException;
import com.gxa.eshop.core.pojo.Goods;
import com.gxa.eshop.core.utils.JsonResult;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Session;
import java.net.URISyntaxException;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    GoodsService goodsService;

    @Resource(name = "simpleJmsTemplate")
    JmsTemplate jmsTemplate;



    @PostMapping("/update")
    public JsonResult update(Goods goods) throws BusinessException, URISyntaxException {

        Integer line = goodsService.updateById(goods);
        JsonResult jsonResult = new JsonResult();
        if(line>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else{
            throw new BusinessException("修改失败,请检查商品编号是否存在");
        }

        // 给前台portal发送一个远程调用请求: 根据id删除缓存
       /* RestTemplate restTemplate = new RestTemplate();
        URI uri = new URI("http://localhost:8080/goods/delete");
        // 参数
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("id", String.valueOf(goods.getId()));
        String s = restTemplate.postForObject(uri, paramMap, String.class);
        System.out.println("s"+s);*/

       // 创建一个线程, 使用线程池, 提取出来-> 中间件(消息中间件)
        // 把要删除的商品缓存的id发送到消息队列上, 存在一个叫 goodsCache 的队列

            jmsTemplate.convertAndSend("goodsCache", goods.getId());


        return jsonResult;
    }
    @PostMapping("/selectAll")
    public JsonResult selectAll(){
        List<Goods> goodsList = goodsService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(goodsList);
        return jsonResult;
    }
}
